<template>
    <div class="app-container">
        <div style="text-align: center;margin-bottom: 30px">
            火情信息{{ createStatus ? '新增' : '更新' }}
        </div>
        <el-form :model="fireInfoForm" ref="fireInfoForm" :rules="fireInfoFormRules" label-width="120px">
            <el-row :gutter="10">
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="火情名称" prop="name">
                        <el-input v-model="fireInfoForm.name" placeholder="请输入火情名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="预警名称" prop="alarmInfoId">
                        <el-select v-model="fireInfoForm.alarmInfoId" placeholder="请选择预警名称" style="width: 100%" :disabled="alarmInfoName">
                            <el-option v-for="(item,index) in alarmInfoList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="详细地址" prop="loaction">
                        <el-input v-model="fireInfoForm.loaction" placeholder="请输入详细地址" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="确定用户" prop="fireUserId">
                        <el-select v-model="fireInfoForm.fireUserId" placeholder="请选择用户"
                                   style="width: 100%">
                            <el-option v-for="(item,index) in userList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="火情起火描述" prop="fireDescription">
                        <el-input v-model="fireInfoForm.fireDescription" placeholder="火情起火描述" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="火灾起火原因" prop="fireCause" :hidden="feedback">
                        <el-input v-model="fireInfoForm.fireCause" placeholder="火灾起火原因" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="救援时间" prop="rescueTime" :hidden="feedback">
                        <el-date-picker
                                v-model="fireInfoForm.rescueTime"
                                type="datetime"
                                placeholder="选择日期"
                                style="width: 100%">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="结束时间" prop="endTime" :hidden="feedback">
                        <el-date-picker
                                v-model="fireInfoForm.endTime"
                                type="datetime"
                                placeholder="选择日期"
                        style="width: 100%">
                        </el-date-picker>
                    </el-form-item>
                </el-col>

            </el-row>
        </el-form>
        <div slot="footer" style="text-align: center" class="dialog-footer">
            <el-button @click="cancelFireInfo">取 消</el-button>
            <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createFireInfo">创 建
            </el-button>
            <el-button :loading="addLoading" v-else type="primary" @click="updateFireInfo">修 改</el-button>
        </div>
    </div>
</template>

<script>
import {getInFireInfoVo,addInFireInfo,updateInFireInfo} from "@/api/fireInfo";
import {getInEnterpriseList,getInUserList} from "@/api/enterprise";
import {getBuildingList} from "@/api/building";
import {getInFireAlarmInfoList} from "@/api/fireAlarmInfo";
import {getInEquipmentMaintenanceVo} from "@/api/equipmentMaintenance";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";


export default {
    name: "infoEdit",
    data() {
        return {
            fireInfoForm: {

            },
            addLoading: false,
            enterpriseDominantList:[],
            buildingList:[],
            alarmInfoList:[],
            userList:[],
            alarmInfoName:true,
            feedback:true,
            fireInfoFormRules:{
                name: [{required: true, message: '火情名称不能为空', trigger: 'blur'}],
                loaction: [{required: true, message: '详细地址不能为空', trigger: 'blur'}],
                fireUserId: [{required: true, message: '确定用户不能为空', trigger: 'blur'}],
                fireDescription: [{required: true, message: '火情起火描述不能为空', trigger: 'blur'}],
            }
        }
    },
    computed: {
        createStatus() {
            return this.$route.query.ucId == null
        },
    },
    created() {
            // this.getInFireAlarmInfoList()
        this.getBuildingList()
        this.getInEnterpriseDominantList()
        this.getInUserList()

        if (!this.createStatus) {
           this.feedback=false
            this.getInFireInfoVo().then(res=>{
               this.fireInfoForm=res.data
                this.getInFireAlarmInfoList(res.data.alarmInfoId)
            }).catch(error=>{

            })
        }else{
            console.log(this.$route.query.eId)
            this.fireInfoForm.equipmentId=this.$route.query.eId
            this.fireInfoForm.alarmInfoId=this.$route.query.alarmInfoId
            this.getInFireAlarmInfoList(this.$route.query.alarmInfoId)
        }



    },
    methods: {
        getInFireInfoVo() {
            return new Promise((resolve, reject) => {
                getInFireInfoVo({ucId:this.$route.query.ucId}).then(res=>{
                    resolve(res)
                }).catch(error=>{
                    reject(error)
                })
            })
        },
        getInEquipmentMaintenanceVo(){
            return new Promise((resolve, reject) => {
                getInEquipmentMaintenanceVo({ucId:this.$route.params.ucId}).then(res=>{
                    resolve(res)
                }).catch(error=>{
                    reject(error)
                })
            })
        },
        cancelFireInfo() {
          window.history.back()
        },
        createFireInfo() {
            this.addLoading = true
            this.$refs['fireInfoForm'].validate(valid => {
                if (valid) {
                    addInFireInfo(this.fireInfoForm).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "添加火情信息成功"
                        })
                        this.cancelFireInfo()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        updateFireInfo() {
            this.addLoading = true
            this.$refs['fireInfoForm'].validate(valid => {
                if (valid) {
                    updateInFireInfo(this.fireInfoForm).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新火情信息成功"
                        })
                        this.cancelFireInfo()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        getInEnterpriseDominantList(){
            getInEnterpriseDominantList({deleted:false}).then(res=>{
                this.enterpriseDominantList = res.data
            }).catch(error=>{

            })
        },
        getBuildingList(){
            getBuildingList({deleted:false}).then(res=>{
                this.buildingList = res.data
            }).catch(error=>{
                alert("获取建筑列表失败")
            })
        },
        getInFireAlarmInfoList(val){
            getInFireAlarmInfoList({deleted:false,ucId:val}).then(res=>{
                this.alarmInfoList = res.data
            }).catch(error=>{
                alert("获取火情预警列表失败")
            })
        },
        getInUserList(){
            getInUserList({deleted:false}).then(res=>{
                this.userList = res.data
            }).catch(error=>{
                alert("获取用户列表失败")
            })
        },

    }

}
</script>
